var batModMap = {   
    'DUPLICATE' : 0,   //副本                 
    'TOWER'     : 1,   //勇闯重楼         
    'KILL'      : 2,   //过关斩将       
    'PVP'       : 3,   //
    'TOMB'      : 4,   //古墓宝藏
    'DIGTREASURE' : 5, // 地下宝藏
    'MYBATTLE'  : 6,   // 个人战役
};

var specialAwardId = 13; 

exports.fight = function(player, req, resp, onHandled) {
    do{ 
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var bossId = Math.floor(req.args.id);
        var aktId = req.uid;
        var tombConf = gConfTomb[bossId];
        if( !tombConf ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var user = player.user;
        if( user.status.level < gConfGlobal.TombLevelLimit ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        if( user.tomb.kill[bossId-1] == 1 ) {
            resp.code = 1; resp.desc = 'boss has been killed'; break;
        }

        var currency = tombConf.Currency;
        var cost = tombConf.Cost;
        if (!player.costResource(currency, cost, bossId)) {
           resp.code = 1; resp.desc = 'not enough resource'; break; 
        }

        var attacker = new Fighter();
        var defender = new Fighter();

        attacker.initWithPlayer(player);
        defender.initWithMonster(batModMap.TOMB, bossId, tombConf);

        var battleReport = attacker.fight(defender);
        if( battleReport.success ) {
            user.tomb.kill[bossId-1] = 1;
            player.markDirty('tomb.kill');
        }
        
        resp.data[currency] = -cost;
        resp.data.battle = battleReport;

    }while(false);

    onHandled();
}

exports.dig = function( player, req, resp, onHandled ) {
    do{
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var bossId = Math.floor(req.args.id);
        var tombConf = gConfTomb[bossId];
        if( !tombConf ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        if( user.status.level < gConfGlobal.TombLevelLimit ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }

        if( user.tomb.kill[bossId-1] == 0 ) {
            resp.code = 1; resp.desc = 'boss not killed'; break;
        }
        
        if( user.tomb.dig[bossId-1] >= gConfGlobal.TombDigNum ) {
            resp.code = 1; resp.desc = 'no dig time left'; break;
        }

        var cashCost = tombConf.DigCash;
        if( cashCost > user.status.cash ) {
            resp.code = 1; resp.desc = 'not enough cash'; break; 
        }

        var weights = {}; 
        var tombRewardConf = gConfTombReward[bossId];
        for( var pos in tombRewardConf ) {
           weights[pos] = tombRewardConf[pos].Weight; 
        }

        var randPos = +common.wRand(weights);
        var awards = [tombRewardConf[randPos].Award1];

        user.tomb.dig[bossId-1] += 1; 
        player.markDirty('tomb.dig');

        // 最后一次挖宝特殊奖励 
        if( user.tomb.dig[bossId-1] >= gConfGlobal.TombDigNum ) {
            awards.push( tombRewardConf[specialAwardId].Award1);
        }

        player.costResource('cash', cashCost, bossId);

        resp.data.awards = player.addAwards(awards);
        resp.data.pos = randPos;
        resp.data.cash = -cashCost;

    }while(false);

    onHandled();
}

exports.get_reward = function( player, req, resp, onHandled ) {
    do{
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var bossId = Math.floor(req.args.id);
        var tombConf = gConfTomb[bossId];
        if( !tombConf ) {
            resp.code = 1; resp.desc = 'invalid args'; break;
        }

        var user = player.user;
        if( user.status.level < gConfGlobal.TombLevelLimit ) {
            resp.code = 1; resp.desc = 'level limit'; break;
        }
        
        if( user.tomb.kill[bossId-1] == 0 ) {
            resp.code = 1; resp.desc = 'boss not killed'; break;
        }

        if( user.tomb.got[bossId-1] == 1 ) {
            resp.code = 1; resp.desc = 'have got'; break;
        }

        var awards = [tombConf.Award1, tombConf.Award2, tombConf.Award3, tombConf.Award4];
        
        user.tomb.got[bossId-1] = 1;
        player.markDirty('tomb.got');

        resp.data.awards = player.addAwards(awards);

    }while(false);

    onHandled();
}

